import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

class GUI 
{


	ActionListener hear=new listen();

	static JTextArea plain=new JTextArea(); //text area for Plain text
	static JTextArea encr=new JTextArea(); //  text area for encrypted text
	static JScrollPane plainS=new JScrollPane(plain);//scroll bar for plain text
	static JScrollPane encrS=new JScrollPane (encr);// scroll bar


	JLabel pLbl=new JLabel();

	JLabel eLbl=new JLabel();
	JLabel passLbl=new JLabel();
	static JPasswordField pass=new JPasswordField();//box for passphrase
	JButton enB =new JButton();
	JButton deB =new JButton();
	//menu setup
	JMenuBar menu =new JMenuBar();//menu
	JMenu mFile = new JMenu("File");//File entry
	JMenuItem mImport = new JMenuItem("Import Key");
	JMenuItem mExport = new JMenuItem("Export Key");

	JMenuItem mQuit = new JMenuItem("Quit");
	JMenu mHelp = new JMenu("Help");
	JMenuItem mAbout = new JMenuItem("About");
	JMenuItem mNKey = new JMenuItem("Generate New Key");
	String[] db = {" v1.3 (Default)"};
	JComboBox dbList = new JComboBox(db);
	JCheckBox hideLbl =new JCheckBox("Hide Label",true);









	public void draw(){
		pLbl.setText("Plain Text:   "); //label for Plain text
		eLbl.setText("Encrypted Text:");
		passLbl.setText("Passphrase:");//Label for passphrase
		enB.setText("Encrypt");//button to submit data
		deB.setText("Decrypt");





		JFrame sett=new JFrame();
		sett.setTitle("jKeyCrypt v1.3"); //window title
		sett.setSize(900,600); //window size
		//create components
		//------------real vars


		//compile the menus
		sett.setJMenuBar(menu);//set 'menu' as the active menu bar

		menu.add(mFile);//add the file menu to the bar
		menu.add(mHelp);

		mFile.add(mImport);//adds the entry 'mImport' to the files tab
		mFile.add(mExport);
		mFile.add(mNKey);
		mFile.add(mQuit);


		mHelp.add(mAbout);





		//add a listener to all the menu items
		mImport.addActionListener(hear);
		mExport.addActionListener(hear);
		mNKey.addActionListener(hear);
		mAbout.addActionListener(hear);
		mQuit.addActionListener(hear);

		//add a listener to the buttons
		dbList.addActionListener(hear);
		enB.addActionListener(hear);
		deB.addActionListener(hear);

		Container sp=sett.getContentPane();
		sp.setLayout(new GridLayout(6,2) );



		//add components

		;//add to the panel
		sp.add(pLbl);
		sp.add(eLbl);
		sp.add(plainS);
		sp.add(encrS);

		sp.add(passLbl);//label for passphrase
		sp.add(pass);
		sp.add(enB);
		sp.add(deB);
		sp.add(dbList);
		sp.add(hideLbl);
		pass.setEchoChar('*');


		sett.setDefaultCloseOperation(sett.EXIT_ON_CLOSE);//enable exit button
		sett.setVisible(true);//show the window

	}
	public static String getPlain()
	{
		return plain.getText();

	}
	public static String getEncrypt()
	{
		return encr.getText();

	}
	public static void setPlain(String pln)
	{
		plain.setText(pln);
	}
	public static void setEnctypt(String enc)
	{
		encr.setText(enc);
	}
	public static String getPass()
	{

		return pass.getText();


	}

}
class listen  implements ActionListener  //Class that reads button pushes
{
	
	private String key=null;
	private int reply=-1;
	String usrInput;
	JFileChooser browser = new JFileChooser();
	jRead mcp =new jRead();
	//read the text file (again) for the new instance of jRead (mcp)
	
	listen()
	{
		try 
		{//we will try to read the key file.
			 File readKey = new File("key");//open key file
			 Scanner	keyText = new Scanner(readKey);
			 String	key = keyText.nextLine();
			 keyText.close();
			 mcp.changeKey(key);
			 key=null;
		}
		catch (FileNotFoundException e) 
		{
			JOptionPane.showMessageDialog(null,  "The File could not be read!","Error!", JOptionPane.ERROR_MESSAGE);
			System.err.println("Could not read from the file!");
		}
	
	}
	
	@Override
	
	public void actionPerformed(ActionEvent did)
	{

		// TODO Auto-generated method stub
		//System.out.println(did.getActionCommand()+" was Pressed   ");
		if (did.getActionCommand().equals("Encrypt"))
		{
		//	GUI.setEnctypt(GUI.getPass());
			GUI.setEnctypt("<"+mcp.encrypt(GUI.getPlain(), GUI.getPass() )+">" );
			
			
		}
		
		
		if (did.getActionCommand().equals("Decrypt"))
		{
			GUI.setPlain(mcp.decrypt(GUI.getEncrypt(), GUI.getPass() ) );
		}
		
		
		if (did.getActionCommand().equals("Generate New Key"))
		{
			String sKeyLen = JOptionPane.showInputDialog("Key File Length:  (250+ suggested) :");
			int keyLen = Integer.valueOf(sKeyLen);
			if (keyLen<1)//to small.
					keyLen=20;	
			key =mcp.mkKey(keyLen);//Generate a key file that is 'keyLen'
			mcp.changeKey(key);
			reply =JOptionPane.showConfirmDialog(null, "Overwrite Local key file?","Key Loaded into memory!", JOptionPane.YES_NO_OPTION);
			if (reply==0)
			{
				try {
				PrintWriter mkKey = new PrintWriter("key"); //open the key file
				mkKey.println(key);
				mkKey.close();
				key=null;
				JOptionPane.showMessageDialog(null,  "Done!","Local Key file overwritten!", JOptionPane.PLAIN_MESSAGE);
				}
				
				catch (FileNotFoundException e1) 
				{
				JOptionPane.showMessageDialog(null,  "Key File I/O Error","Could not write file!", JOptionPane.ERROR_MESSAGE);
				key=null;
				System.exit(0); 
				e1.printStackTrace();
				}
				key=null;
			
			}
			key=null;
		}
		
		
		if (did.getActionCommand().equals("Import Key"))
		{
			
			browser.showOpenDialog(null);
			usrInput = browser.getSelectedFile().getPath();
			try {//we will try to read the key file.
					File readKey = new File(usrInput);//open key file
					Scanner	keyText = new Scanner(readKey);
					key = keyText.nextLine();
					keyText.close();
					mcp.changeKey(key);
					reply =JOptionPane.showConfirmDialog(null, "Overwrite Local key file?","Key Loaded into memory!", JOptionPane.YES_NO_OPTION);
					if (reply==0)
					{
						try {
						PrintWriter mkKey = new PrintWriter("key"); //open the key file
						mkKey.println(key);
						mkKey.close();
						key=null;
						JOptionPane.showMessageDialog(null,  "Done!","Local Key file overwritten!", JOptionPane.PLAIN_MESSAGE);
						}
						
						catch (FileNotFoundException e1) 
						{
						JOptionPane.showMessageDialog(null,  "Key File I/O Error","Could not write file!", JOptionPane.ERROR_MESSAGE);
						key=null;
						System.exit(0); 
						e1.printStackTrace();
						}
						key=null;
					}
					key=null;
				}
			catch (FileNotFoundException e) 
			{
			JOptionPane.showMessageDialog(null,  "The File could not be read!","Error!", JOptionPane.ERROR_MESSAGE);
			System.err.println("Could not read from the file!");
			}			
		}
		
		
		if (did.getActionCommand().equals("Export Key"))
		{
			browser.showSaveDialog(null);
			usrInput = browser.getSelectedFile().getPath();
			mcp.exportKey(usrInput);
			
		}
		
		
		if (did.getActionCommand().equals("About"))
		{
			JOptionPane.showMessageDialog(null,  "By BOBdotEXE\nhttp://code.google.com/p/jkeycrypt/","jKeyCrypt v1.3", JOptionPane.INFORMATION_MESSAGE);
		}
		
		
		if (did.getActionCommand().equals("Quit"))
		{
			System.exit(0);
		}

	}
	
	
}